import os
from py_clob_client.constants import POLYGON
from py_clob_client.client import ClobClient
from py_clob_client.clob_types import OrderArgs, PartialCreateOrderOptions, OrderType, TickSize
from py_clob_client.order_builder.constants import BUY

host = "https://clob.polymarket.com"
key = "0x4c9da093551439ae10e5232b6ebae9921fb5c2d6750b182498566854555a864e"  # os.getenv("0x4c9da093551439ae10e5232b6ebae9921fb5c2d6750b182498566854555a864e")
chain_id = POLYGON

# Create CLOB client and get/set API credentials
client = ClobClient(host, key=key, chain_id=chain_id, funder="0x3003fe4bF9AA77b440edC5608eC5CA5Ab03B4005",
                    signature_type=2)
creds = client.create_or_derive_api_creds()
client.set_api_creds(creds)

# json_order = order_to_json(
#     order=builder.create_order(
#         order_args=OrderArgs(
#             token_id="100",
#             price=0.5,
#             size=100,
#             side=BUY,
#         ),
#         options=CreateOrderOptions(tick_size="0.1"),
#     ),
#     owner=owner,
#     orderType=OrderType.GTC,
# )

# Create and sign an order buying 100 YES tokens for 0.50c each
resp = client.get_price(
    "27477810110744608987656101800471120045579900492893583840456667634914344288655",
    "BUY",
)
OrderArgs = OrderArgs(
    maker_amount="100000",
    taker_amount="105200",
    side=BUY,
    token_id="27477810110744608987656101800471120045579900492893583840456667634914344288655"
)
Options = PartialCreateOrderOptions(neg_risk=True)  # , tick_size="0.01"
signed_order = client.create_order(OrderArgs, Options)
# print("makerAmount", signed_order.order.value.get())                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               s.get('makerAmount'))  # makerAmount takerAmount
# print("takerAmount", signed_order.order.values.get('takerAmount'))
# 105200 100000
try:
    resp = client.post_order(signed_order, OrderType.FOK)
    print(resp)
except Exception as e:
    print(e.__str__())

# try:
#     client.create_api_key(0)
# except:
#     pass
# try:
#     print(client.get_api_keys())
# except:
#     pass
# resp = client.create_and_post_order(OrderArgs)
# print(client.get_address())
# print(resp)
